package com.ams.aisino;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.impl.Base64Codec;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JJWTUtils {

    private static String SECRET_KEY="qihao@qihao";

    /**
     * 创建JWT
     * @param type 数据类型，例如JWT
     * @param subject 主题
     * @param ttlMillis 过期时长
     * @return
     * @throws Exception
     */
    public static String create (String type, String subject, long ttlMillis) throws Exception {

        SignatureAlgorithm alg =  SignatureAlgorithm.HS256;  //签名算法

        long startMillis = System.currentTimeMillis();

        Date now = new Date(startMillis); //签名时间

        Map<String,Object> claims = new HashMap<String, Object>();  //创建有效载荷（playload）
        claims.put("oid", "b08f86af-35da-48f2-8fab-cef3904660bd");
        claims.put("org","www.ramoster.com" );

        SecretKey secret = key();

        JwtBuilder builder = Jwts.builder()
                .setClaims(claims)
                .setId(type)
                .setIssuedAt(now)
                .setSubject(subject)   //主题，是JWT的主题，即JWT的拥有者（如UID,email,roles等）
                .signWith(alg, secret);

        if (ttlMillis>0){
            long expMillis = startMillis+ttlMillis;
            Date exp = new Date(expMillis);
            builder.setExpiration(exp);
        }

        return builder.compact();
    }

    /**
     * 生成签名秘钥
     * @return
     */
    public static SecretKey key(){
        byte[] encodeKey = Base64Codec.BASE64.decode(SECRET_KEY);
        SecretKey key = new SecretKeySpec(encodeKey, 0, encodeKey.length, "AES");
        return key;
    }


    /**
     * 解析JWT
     * @param jwt
     * @return
     * @throws Exception
     */
    public static Claims parse(String jwt) throws Exception{

        SecretKey secretKey = key();  //获取签名秘钥

        Claims claims = Jwts.parser()   //开始解析JWT
                .setSigningKey(secretKey)  //设置秘钥信息
                .parseClaimsJws(jwt).getBody();  //解析主题信息

        return claims;
    }

}
